/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.yucc.girlimages.ui;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.Loader;
import android.view.View;
import android.widget.AdapterView;

import com.yucc.girlimages.GirlsApplication;
import com.yucc.girlimages.io.StreamLoader;
import com.yucc.girlimages.io.upyun.FolderItem;
import com.yucc.girlimages.util.Logger;

/**
 * The main fragment that powers the DetailThumbsActivity screen. Fairly straight
 * forward GridView implementation with the key addition being the ImageWorker
 * class w/ImageCache to load children asynchronously, keeping the UI nice and
 * smooth and caching thumbnails for quick retrieval. The cache is retained over
 * configuration changes like orientation change so the images are populated
 * quickly as the user rotates the device.
 */
public class DetailThumbsFragment extends BaseThumbsFragment implements
		AdapterView.OnItemClickListener, LoaderCallbacks<ArrayList<FolderItem>> {
	protected static String TAG = "ImageGridDetailFragment";

	protected static final String EXTRA_ITEM_NUMBER = "extra_item_number";
	protected static final String EXTRA_URL = "extra_url";
	protected static final String EXTRA_TOAST_ENABLED = "extra_toast_enabled";

	private static final int STREAM_LOADER_ID = 0;
	//可作为分类信息,或者列表请求地址
	private String mCategory;
	private String mImageDir ;
	private int mItemPosition ;
	private boolean mToastEnabled ;
	private StreamLoader mStreamLoader ;

	public static final DetailThumbsFragment newInstance(Bundle args) {
		final DetailThumbsFragment fragment = new DetailThumbsFragment();
		fragment.setArguments(args);
		return fragment;
	}

	public static final DetailThumbsFragment newInstance(String category,int position,boolean showToast) {
		Bundle args = new Bundle();
		args.putString(EXTRA_CATEGORY, category);
		args.putInt(EXTRA_ITEM_NUMBER, position) ;
		args.putBoolean(EXTRA_TOAST_ENABLED, showToast) ;
		return newInstance(args);
	}

	/**
	 * Empty constructor as per the Fragment documentation
	 */
	public DetailThumbsFragment() {
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setHasOptionsMenu(true);
		
		if(getArguments() != null){
			mCategory = getArguments().getString(EXTRA_CATEGORY);
			mToastEnabled = getArguments().getBoolean(EXTRA_TOAST_ENABLED) ;
			mItemPosition = getArguments().getInt(EXTRA_ITEM_NUMBER) ;
			if(mItemPosition == 0){
				mImageDir = mCategory ;
			}else{
				mImageDir = mCategory + "/" + mItemPosition ;
			}
			Logger.d(TAG, "mPosition="+mItemPosition+",showToast="+mToastEnabled) ;
		}
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		getLoaderManager().initLoader(STREAM_LOADER_ID, null, this);
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
		final Intent i = new Intent(getActivity(), DetailActivity.class);
		i.putExtra(DetailActivity.EXTRA_IMAGE_POSITION, (int)id);
		i.putExtra(DetailActivity.EXTRA_ADAPTER, getExtImageWorkerAdapter());
		i.putExtra(DetailActivity.EXTRA_CATEGORY, mCategory) ;
		startActivity(i);
	}

	@Override
	public Loader<ArrayList<FolderItem>> onCreateLoader(int arg0, Bundle arg1) {
		mStreamLoader = new StreamLoader(getSherlockActivity(), mCategory != null ? mImageDir :"")
		.setToastEnable(mToastEnabled).setCategoryRequest(false)
		.setIsLocal(mCategory == null ? true : false);
		return mStreamLoader ;
	}

	@Override
	public void onLoadFinished(Loader<ArrayList<FolderItem>> arg0,
			ArrayList<FolderItem> list) {
		
		//设置toast提示
		StreamLoader loader = (StreamLoader)arg0 ;
		if(loader.isToastEnable() && getExtImageWorkerAdapter().getSize() == 0){
			((GirlsApplication)getSherlockActivity().getApplication()).showToast(loader.getFolderName());
			getSherlockActivity().getSupportActionBar().setTitle(loader.getFolderName()) ;
		}
		if (list != null) {
			getExtImageWorkerAdapter().setList(list);
		}else {
			getSherlockActivity().getSupportActionBar().setTitle(mCategory == null ? "暂无收藏的美女" : "没有图片") ;
		}
		getImageAdapter().notifyDataSetChanged();
	}

	@Override
	public void onLoaderReset(Loader<ArrayList<FolderItem>> arg0) {

	}
}
